package servlet;

import com.aliyun.oss.common.utils.StringUtils;
import dao.ProductDAO;
import entity.Product;
import org.bson.types.ObjectId;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "productUpdate",value = "/product/update")
public class ProductUpdateServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String id=req.getParameter("id");
        ObjectId oid=new ObjectId(id);
        String name=req.getParameter("name");
        String price=req.getParameter("price");
        String sum=req.getParameter("sum");
        String imageUrl= req.getParameter("imageUrl");
//        System.out.println(" update  imageUrl: " + imageUrl);
        double price1=Double.parseDouble(price.trim());
        long sum1=Long.parseLong(sum.trim());
        String info=req.getParameter("info");
        ProductDAO productDao=new ProductDAO();
        Product p=productDao.findById(oid);
        p.setName(name);
        p.setPrice(price1);
        p.setSum(sum1);
        p.setInfo(info);
        if(!StringUtils.isNullOrEmpty(imageUrl))p.setImageUrl(imageUrl);
//        System.out.println("imageUrl ======  " + p.getImageUrl());
        productDao.update(p);
        resp.sendRedirect("/Mall/product/list");
    }
}
